﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BF.IBLL.BaseSystem;
using BF.Infrastructure;
using BF.Model;
using BF.IDAL.BaseSystem;
using BF.Membership.MembershipEF;
using BF.Membership.BLL;
using System.Linq.Expressions;
using System.Web.Security;
using BF.Membership;

namespace BF.BLL.BaseSystem
{
    public class Sys_RoleInfoBLL : Isys_RoleInfoBLL
    {
        private readonly IUnitOfWork<WSJD_BaseFrameworkEntities> uwork;
        private readonly Isys_RoleInfoDAL iDal;
        private readonly Isys_UserInfoDAL isys_UserInfoDAL;
        private readonly AspnetRolesBLL rolesBLL = new AspnetRolesBLL();

        public Sys_RoleInfoBLL(IUnitOfWork<WSJD_BaseFrameworkEntities> uwork,
            Isys_RoleInfoDAL iDal,
            Isys_UserInfoDAL isys_UserInfoDAL)
        {
            this.uwork = uwork;
            this.iDal = iDal;
            this.isys_UserInfoDAL = isys_UserInfoDAL;
        }


        public RoleView GetRoleViewByName(string roleName)
        {
            aspnet_Roles aspnet_Roles = rolesBLL.GetModelByName(roleName);
            Guid gRoleId = aspnet_Roles.RoleId;
            sys_RoleInfo sysRoleInfo = iDal.GetModelByCondition(P => P.gRoleId == gRoleId);
            RoleView roleView = new RoleView()
            {
                ApplicationId = aspnet_Roles.ApplicationId,
                gRoleId = aspnet_Roles.RoleId,
                RoleName = aspnet_Roles.RoleName,
                LoweredRoleName = aspnet_Roles.LoweredRoleName,
                Description = aspnet_Roles.Description,
                gCreateUserId = sysRoleInfo.gCreateUserId,
                sCreateLoginName = sysRoleInfo.sys_UserInfo.sLoginName,
            };
            return roleView;
        }

        public bool AddRole(RoleView roleView, sys_UserInfo user)
        {
            aspnet_Roles aspnetRoles = new aspnet_Roles()
            {
                RoleName = roleView.RoleName,
                Description = roleView.Description,
            };
            sys_RoleInfo roleInfo = new sys_RoleInfo()
            {
                gCreateUserId = user.gUserID,
            };

            if (rolesBLL.CreateRoles(aspnetRoles, user.sLoginName))
            {
                aspnetRoles = rolesBLL.GetModelByName(roleView.RoleName);
                roleInfo.gRoleId = aspnetRoles.RoleId;
                iDal.Add(roleInfo);
                return (uwork.Commit() > 0);
            }
            else
                throw new Exception("角色创建失败");
        }

        public bool DeleteRole(Guid roleId)
        {
            iDal.Delete(p => p.gRoleId == roleId);
            return (uwork.Commit() > 0);
        }

        public bool UpdateRole(RoleView roleView, sys_UserInfo user)
        {
            aspnet_Roles aspnetRoles = rolesBLL.GetModelByName(roleView.RoleName);
            aspnetRoles.ApplicationId = roleView.ApplicationId;
            aspnetRoles.RoleId = roleView.gRoleId;
            aspnetRoles.RoleName = roleView.RoleName;
            aspnetRoles.LoweredRoleName = roleView.LoweredRoleName;
            aspnetRoles.Description = roleView.Description;

            Guid gRoleId = roleView.gRoleId;
            sys_RoleInfo roleInfo = iDal.GetModelByCondition(P => P.gRoleId == gRoleId);
            roleInfo.gRoleId = roleView.gRoleId;
            roleInfo.gCreateUserId = user.gUserID;
            return (uwork.Commit() > 0);
        }

        public int GetViewRowCount(Expression<Func<sys_RoleInfo, bool>> where)
        {
            throw new NotImplementedException();
        }

        public List<RoleView> GetViewList(
            Expression<Func<sys_RoleInfo, bool>> where,
            Expression<Func<sys_RoleInfo, object>> orderBy,
            PageModel pageModel, bool isAscending)
        {
            throw new NotImplementedException();
        }

        public List<RoleView> GetViewList(Guid userId)
        {
            string[] roles = Roles.GetRolesForUser();
            List<aspnet_Roles> roleList = rolesBLL.GetList(roles);
            Guid[] roleIds = roleList.Select(P => P.RoleId).ToArray();
            List<sys_RoleInfo> roleInfos = iDal.GetList(P => roleIds.Contains(P.gRoleId));
            return roleList.Join(roleInfos,
                P => P.RoleId,
                Q => Q.gRoleId,
                (P, Q) => new RoleView()
                {
                    ApplicationId = P.ApplicationId,
                    gRoleId = P.RoleId,
                    RoleName = P.RoleName,
                    LoweredRoleName = P.LoweredRoleName,
                    Description = P.Description,
                    gCreateUserId = Q.gCreateUserId,
                    sCreateLoginName = Q.sys_UserInfo.sLoginName,
                    IsCurrentUserCreate = (userId == Q.gCreateUserId)
                }).Where(p => p.RoleName != "系统管理员")
                .ToList();
        }

        public List<sys_RoleInfo> GetRoleCreateByUserId(Guid userId)
        {
            return iDal.GetList(p => p.gCreateUserId == userId);
        }

        public aspnet_Roles GetRoleById(Guid roleId)
        { return rolesBLL.GetModel(roleId); }
    }
}
